package com.zsh.wanandroid.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.zsh.wanandroid.ui.view.RvViewHolder;

import java.util.List;

/**
 * @author zsh
 * @date 2020/3/13.
 * 功能：RecyclerViewAdapter基类
 */

public abstract class CommonRecyclerViewAdapter<T> extends RecyclerView.Adapter<RvViewHolder> {

    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;


    public CommonRecyclerViewAdapter(Context context, int layoutId, List<T> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;
    }

    public void setData(List<T> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public RvViewHolder onCreateViewHolder(final ViewGroup parent, int viewType)
    {
        RvViewHolder viewHolder = RvViewHolder.get(mContext, parent, mLayoutId);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RvViewHolder holder, int position)
    {
        //holder.updatePosition(position);
        convert(holder, mDatas.get(position), position);
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size();
    }

    public abstract void convert(RvViewHolder holder, T t, int position);

}
